import { BaseItem } from "../../common/BaseItem";
import { ObjCreate } from "../../common/ObjCreate";
import { ImagesConst } from "../../../resource/ImagesConst";
import { DragEvent } from "../../../../../com/mui/events/DragEvent";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { GuildEventName } from "../GuildEventName";
import { ItemData } from "../../../resource/info/ItemData";
import { EPlayerItemPosType } from "../../../../../Message/Public/EPlayerItemPosType";
import { AssignItemWindow } from "./AssignItemWindow";
import { DataEvent } from "../../../events/DataEvent";
import { ItemsUtil } from "../../shortcuts/item/ItemsUtil";
type int = number;
//class AssignBaseItem
    
    export  class AssignBaseItem extends BaseItem
    {
        constructor()
        {
            super();
            ObjCreate.createBg(0, 0, 38, 38, this, ImagesConst.PackItemBg);
            this.setSize(32, 32);
            _bitmap.x = 2;
            _bitmap.y = 2;
            this.addEventListener(DragEvent.Event_Move_In, this.onMoveInHandler);
            this.addEventListener(DragEvent.Event_Throw_goods, this.onThrowHandler);
            this.addEventListener(flash.events.MouseEvent.CLICK, this.onAssignItemClickHandler);
            Dispatcher.addEventListener(GuildEventName.WarehouseConfirmAssignNum, this.onWarehouseConfirmAssignNumHandler);
            return;
        }

        /* internal  */onMoveInHandler(arg1: DragEvent): void
        {
            var loc1=null;
            var loc3=null;
            var loc2=arg1.dragItem;
            if (loc2 && loc2 is AssignBaseItem) 
            {
                return;
            }
            if (typeof arg1.dragSouce === "itemdata") 
            {
                loc3 = arg1.dragSouce as ItemData;
                this._maxAmount = loc3.itemAmount;
                loc1 = new ItemData(loc3.itemCode);
                loc1.data.uid = loc3.uid;
                loc1.posType = EPlayerItemPosType._EPlayerItemPosTypeGuildWarehouse;
                loc1.itemAmount = loc3.itemAmount;
            }
            this.confirmAssign(loc1);
            return;
        }

        public confirmAssign(arg1: ItemData): void
        {
            var loc1=null;
            var loc2=0;
            if (arg1 && arg1.posType == EPlayerItemPosType._EPlayerItemPosTypeGuildWarehouse) 
            {
                loc1 = AssignItemWindow.getExistAssignItem(arg1);
                if (loc1 != null) 
                {
                    if (arg1.itemAmount > 1) 
                    {
                        Dispatcher.dispatchEvent(new DataEvent(GuildEventName.WarehouseOpenAssignNumWindow, {"posIndex": loc1.posIndex, "itemData": arg1}));
                    }
                    else if (ItemsUtil.isOverlay(arg1.itemInfo.item)) 
                    {
                        loc1.itemData.itemAmount = loc1.itemData.itemAmount + 1;
                    }
                }
                else if (arg1.itemAmount > 1) 
                {
                    Dispatcher.dispatchEvent(new DataEvent(GuildEventName.WarehouseOpenAssignNumWindow, {"posIndex": this.posIndex, "itemData": arg1}));
                }
                else 
                {
                    this.itemData = arg1;
                }
            }
            return;
        }

        /* internal  */onThrowHandler(arg1: DragEvent): void
        {
            var loc1=arg1.dragItem;
            if (loc1 && loc1 is AssignBaseItem) 
            {
                (loc1 as AssignBaseItem).itemData = null;
            }
            return;
        }

        /* internal  */onWarehouseConfirmAssignNumHandler(arg1: DataEvent): void
        {
            var loc3=0;
            var loc1=arg1.data["itemData"] as ItemData;
            var loc2=arg1.data["posIndex"] as int;
            if (loc1 && loc2 == this.posIndex) 
            {
                if (this.itemData) 
                {
                    loc3 = this.itemData.itemAmount + loc1.itemAmount;
                }
                else 
                {
                    loc3 = loc1.itemAmount;
                }
                if (loc3 < this._maxAmount) 
                {
                    loc1.itemAmount = loc3;
                }
                else 
                {
                    loc1.itemAmount = this._maxAmount;
                }
                this.itemData = loc1;
            }
            return;
        }

        /* internal  */onAssignItemClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (this.itemData) 
            {
                this.itemData = null;
            }
            return;
        }

        public set maxAmount(arg1: int)
        {
            this._maxAmount = arg1;
            return;
        }

        /* internal */ /* var */_maxAmount: int;
    }
